package davion.study.summercloud.controller;

import davion.study.summercloud.model.FileFolder;
import davion.study.summercloud.model.FileStore;
import davion.study.summercloud.model.User;
import davion.study.summercloud.service.FileFolderService;
import davion.study.summercloud.service.FileStoreService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * @author: Davion
 * @date: 2020/6/17
 * @description:
 */
@Controller
public class FolderController {
    private Logger logger = LoggerFactory.getLogger(FolderController.class);

    @Autowired
    FileStoreService fileStoreService;

    @Autowired
    FileFolderService fileFolderService;

    @PostMapping("/addFolder")
    public String addFolder(HttpServletRequest request, FileFolder folder, Model model){
        User user = (User) request.getSession().getAttribute("user");
        if (user.getLevel() == 3) {
            return  "redirect:/files?error=6&fId=" + folder.getParentId();
        }
        FileStore fileStore = fileStoreService.getStoreByUserId(user.getId());
        folder.setStoreId(fileStore.getId());
        folder.setCreateTime(System.currentTimeMillis());
        folder.setModifyTime(System.currentTimeMillis());
        List<FileFolder> fileFolders;
        if (folder.getParentId() == 0) {
            fileFolders = fileFolderService.getRootFoldersByParentId(fileStore.getId());
        } else {
            fileFolders = fileFolderService.getFoldersByParentId(folder.getParentId());
        }
        for (FileFolder fileFolder : fileFolders) {
            if (fileFolder.getName().equals(folder.getName())){
                logger.error("文件夹已存在，添加失败！！！");
                return  "redirect:/files?error=1&fId=" + folder.getParentId();
            }
        }
        Boolean addSuccess = fileFolderService.addFolder(folder);
        if (addSuccess){
            logger.info("文件夹添加成功！！！" + folder);
            return "redirect:/files?fId=" + folder.getParentId();
        }
        return "redirect:/files?error=3&fId=" + folder.getParentId();
    }

    @PostMapping("/updateFolder")
    public String updateFolder(HttpServletRequest request, FileFolder folder, Model model) {
        FileFolder dbFolder = fileFolderService.getFoldersById(folder.getId());
        User user = (User) request.getSession().getAttribute("user");
        if (user.getLevel() == 3) {
            return  "redirect:/files?error=6&fId=" + dbFolder.getParentId();
        }
        dbFolder.setName(folder.getName());
        List<FileFolder> folders = fileFolderService.getFoldersByParentId(dbFolder.getParentId());
        for (FileFolder fileFolder : folders) {
            if (fileFolder.getName().equals(dbFolder.getName())) {
                logger.info("重命名文件夹失败，文件夹已存在！！！");
                return "redirect:/files?error=2&fId=" + dbFolder.getParentId();
            }
        }
        Boolean updateSuccess = fileFolderService.updateFolder(dbFolder);
        if (updateSuccess){
            logger.info("文件夹重命名成功！！！" + dbFolder);
            return "redirect:/files?fId=" + dbFolder.getParentId();
        }
        return "redirect:/files?error=3&fId=" + dbFolder.getParentId();
    }

    @GetMapping("/deleteFolder")
    public String deleteFolder(HttpServletRequest request, Integer fId){
        FileFolder folder = fileFolderService.getFoldersById(fId);
        User user = (User) request.getSession().getAttribute("user");
        if (user.getLevel() == 3) {
            return  "redirect:/files?error=6&fId=" + folder.getParentId();
        }
        try {
            fileFolderService.deleteFolder(folder);
        } catch (IOException e) {
            logger.error("文件夹清空失败！！！");
            return "redirect:/files";
        }
        return folder.getParentId() == 0 ? "redirect:/files" : "redirect:/files?fId=" + folder.getParentId();
    }
}
